﻿<cfscript>
/** 
* @hint "teacherListener - Teacher"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.teacherListener" 
	output=false 
	depends="chineseFactory, sessionAdvice, stringAdvice, datetimeAdvice" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "教师自己更新联系信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateTeacherContract( required MachII.framework.Event event ) output=false {
		
		var teacherId = getsessionAdvice().getAutherUserID();
		
		var sql = "";
		var queryObj = "";
		var rs_check = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 读取教师档案 */
			sql = "SELECT tch_name, tch_pyidx, tch_sex, tch_idcard, tch_email, tch_tel FROM t_teacher WHERE tch_id = :teacherId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
			
			var rs_teacher = queryObj.execute( sql=sql ).getResult();
			
			
			/* 默认值 */
			var teacherName = rs_teacher.tch_name;
			var teacherPinyin = rs_teacher.tch_pyidx;
			var teacherSex = rs_teacher.tch_sex;
			var teacherIDCard = rs_teacher.tch_idcard;
			var teacherEmailAddress = rs_teacher.tch_email;
			var teacherTelephone = rs_teacher.tch_tel;

			/* 姓名 */
			if ( event.isArgDefined("UserName") ) {
				teacherName = trim( event.getArg("UserName") );
			}

			/* 默认拼音 */
			teacherPinyin = chineseFactory.getPinyinIndex( teacherName );

			/* 性别 */
			if ( event.isArgDefined("TeacherSex") ) { 
				teacherSex = event.getArg("TeacherSex"); 
			}

			/* 身份证号 */
			if ( event.isArgDefined("TeacherIDCard") ) { 
				teacherIDCard = trim( event.getArg("TeacherIDCard") ); 
			}
			
			/* 电子邮件 */
			var teacherEmail = trim( event.getArg("EmailAddress") );
			if ( len( teacherEmail ) and getstringAdvice().isEmail( teacherEmail ) ) {
				
				/* 检查电子邮件唯一性 */
				sql = "SELECT tch_id FROM t_teacher WHERE tch_email = :teacherEmail ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="teacherEmail", value=teacherEmail, cfsqltype="cf_sql_varchar" );
			
				rs_check = queryObj.execute( sql=sql ).getResult();
				
				if ( rs_check.recordCount eq 0 or rs_check.tch_id eq teacherId ) {
					teacherEmailAddress = teacherEmail;
				}

			}
			
			/* 电话号码 */
			if ( event.isArgDefined("TeacherTelephone") ) { 
				teacherTelephone = trim( event.getArg("TeacherTelephone") ); 
			}
			
			/* 更新教师档案 */
			sql = "UPDATE t_teacher 
					SET 
						tch_name = :teacherName,
						tch_pyidx = :teacherPinyin, 
						tch_sex = :teacherSex, 
						tch_idcard = :teacherIDCard, 
						tch_email = :teacherEmailAddress, 
						tch_tel = :teacherTelephone 
					WHERE 
						tch_id = :teacherId ";

			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );

			queryObj.addParam( name="teacherName", value=teacherName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="teacherPinyin", value=teacherPinyin, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="teacherSex", value=teacherSex, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="teacherIDCard", value=teacherIDCard, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="teacherEmailAddress", value=teacherEmailAddress, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="teacherTelephone", value=teacherTelephone, cfsqltype="cf_sql_varchar" );

			queryObj.execute( sql=sql );
			
			var password = trim( event.getArg("password") );
			
			if( password neq ""){
				sql = "UPDATE t_user
						SET 
							user_pass = :userPass 
						WHERE 
							user_id = :teacherId ";
	
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
	
				queryObj.addParam( name="userPass", value=password, cfsqltype="cf_sql_varchar" );
	
				queryObj.execute( sql=sql );
			}
			
			
			
			
			event.setArg( "UserName", teacherName );
			
		}

	}
	
	/**
	* @hint "更新指定教师的档案信息 包括学历、类别、职称"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateTeacherProfile( required MachII.framework.Event event ) output=false {
		
		
		var sql = "";
		var queryObj = "";
	
		var tchId = event.getArg("TeacherID");
		
		var depId = event.getArg("InstituteID");
		var typeId = event.getArg("TypeCode");
		//var tpId = event.getArg("TechnicalPositionID");
		var eduId = event.getArg("EduCode");
		
		var pointManager = event.getArg("pointManager");
		var paperManager = event.getArg("paperManager");
		
		var loginName = event.getArg("userLogin");

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			
			/*检查登录名是否有重复*/
			
			var bool = true;
			var fxNum = "";
			
			if( bool ){
			
				sql = "SELECT * FROM t_user where user_login = :userLogin AND user_id != :teacherId";
				
				queryObj = new Query( datasource=application.dnsSlave );
				
				queryObj.addParam( name="userLogin", value=loginName, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="teacherId", value=tchId, cfsqltype="cf_sql_varchar" );
				
				rs_user = queryObj.execute( sql=sql ).getResult();
				
				if( rs_user.recordCount ){
					
					sql = "select SEQ_userlogin.nextval fxNum from dual";
					
					queryObj = new Query( datasource=application.dnsSlave );
									
					rs_userLogin = queryObj.execute( sql=sql ).getResult();	
					
					fxNum = rs_userLogin.fxNum;
					
					loginName = loginName & fxNum; //给重复的登录号添加一个后缀
					
				}else{
					bool = false;	
				}
			
			}
			
			sql = "SELECT a.tch_name,
						   a.tch_sex,
						   a.tch_idcard,
						   a.tch_tel,
						   a.tch_email  
					FROM t_teacher a 
					WHERE a.tch_id = :tchId ";
					
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="tchId", value=tchId, cfsqltype="cf_sql_varchar" );
		
			var rs_teacher = queryObj.execute( sql=sql ).getResult();
			
			/* 部门 */
			var InstituteId = depId;

			/* 教师类别 */
			var TeacherType = typeId;

			/* 学历 */
			var TeacherEducational = eduId;

			/* 职称 */
			//var TechnicalPosition = tpId;
			var TeacherName = rs_teacher.tch_name;
			
			var teacherName = trim( event.getArg("TeacherName") );
			if ( len(teacherName) and teacherName neq rs_teacher.tch_name ) {
				TeacherName = teacherName;
			}
			
			/* 默认拼音 */
			var TeacherPinyin =  chineseFactory.getPinyinIndex( rs_teacher.tch_name ) ;
			var TeacherSex = rs_teacher.tch_sex;
			/* 性别 */
			if ( event.isArgDefined("TeacherSex") ) { 
				TeacherSex = event.getArg("TeacherSex") ; 
			}
			var TeacherIDCard = rs_teacher.tch_idcard;
			/* 身份证号 */
			if ( event.isArgDefined("TeacherIDCard") ) { 
				TeacherIDCard = trim( event.getArg("TeacherIDCard") ); 
			}
			var TeacherEmail = rs_teacher.tch_email ;
			/* 电子邮件 */
			var teacherEmail = trim( event.getArg("EmailAddress") );
			
			var TeacherTelephone = rs_teacher.tch_tel;
			/* 电话号码 */
			if ( event.isArgDefined("TeacherTelephone") ) { 
				TeacherTelephone = trim( event.getArg("TeacherTelephone") ); 
			}
			
			sql = "UPDATE  t_teacher 
					SET		institute_id=:InstituteId,
							tch_type=:TeacherType,
							edu_id=:TeacherEducational,
							tch_name=:TeacherName,
							tch_pyidx=:TeacherPinyin,
							tch_sex=:TeacherSex,
							tch_idcard=:TeacherIDCard,
							tch_email=:TeacherEmail,
							tch_tel=:TeacherTelephone,
							point_manager = :pointManager,
							paper_manager = :paperManager   
					WHERE tch_id = :tchId 
					";
					
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="InstituteId", value=InstituteId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherType", value=TeacherType, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TeacherEducational", value=TeacherEducational, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TeacherName", value=TeacherName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherPinyin", value=TeacherPinyin, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherSex", value=TeacherSex, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TeacherIDCard", value=TeacherIDCard, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherEmail", value=TeacherEmail, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherTelephone", value=TeacherTelephone, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="tchId", value=tchId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="pointManager", value=pointManager, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="paperManager", value=paperManager, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
			//登录信息
			
			sql = "UPDATE t_user SET role = :role,user_login = :userLogin WHERE user_id = :teacherId ";
	
		    queryObj = new Query( datasource=application.dnsMaster );
		    
		    queryObj.addParam( name="teacherId", value=tchId, cfsqltype="cf_sql_varchar" );

		    queryObj.addParam( name="role", value=event.getArg("role"), cfsqltype="cf_sql_char" );
		    queryObj.addParam( name="userLogin", value=loginName, cfsqltype="cf_sql_varchar" );

		    queryObj.execute( sql=sql );
			
			var password = trim( event.getArg("password") );
			
			if( password neq ""){
				sql = "UPDATE t_user
						SET 
							user_pass = :userPass 
						WHERE 
							user_id = :teacherId ";
	
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="teacherId", value=tchId, cfsqltype="cf_sql_varchar" );
	
				queryObj.addParam( name="userPass", value=password, cfsqltype="cf_sql_varchar" );
	
				queryObj.execute( sql=sql );
			}

		}
		
	}
	
	
	/**
	* @hint "添加教师信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function teacheAdd( required MachII.framework.Event event ) output=false {
		
		
		var sql = "";
		var queryObj = "";
	
		//var tchId = event.getArg("TeacherID");
		
		var depId = event.getArg("InstituteID");
		var TeacherType = event.getArg("TypeCode");
		var tpId = event.getArg("TechnicalPositionID");
		var eduId = event.getArg("EduCode");
		
		var loginName = event.getArg("username");
		var password = event.getArg("password");
		
		var date = dateFormat(now(),"YYYYMMDDHHMMSS");

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var bool = true;
			var fxNum = "";
			
			if( bool ){
			
				sql = "SELECT * FROM t_user where user_login = :userLogin";
				
				queryObj = new Query( datasource=application.dnsSlave );
				
				queryObj.addParam( name="userLogin", value=loginName, cfsqltype="cf_sql_varchar" );
				
				rs_user = queryObj.execute( sql=sql ).getResult();
				
				if( rs_user.recordCount ){
					
					sql = "select SEQ_userlogin.nextval fxNum from dual";
					
					queryObj = new Query( datasource=application.dnsSlave );
									
					rs_userLogin = queryObj.execute( sql=sql ).getResult();	
					
					fxNum = rs_userLogin.fxNum;
					
					loginName = loginName & fxNum; //给重复的登录号添加一个后缀
					
				}else{
					bool = false;	
				}
			
			}
			
			sql = "select SEQ_teacher.nextval tchId from dual";
					
			queryObj = new Query( datasource=application.dnsSlave );
							
			rs_teacher = queryObj.execute( sql=sql ).getResult();
			
			var teacherId = rs_teacher.tchId;
			
			
			sql = "INSERT INTO t_teacher(
					  tch_id,institute_id,tch_type, tp_id, edu_id,tch_name, 
					  tch_pyidx,tch_sex,tch_idcard,
					  tch_email,tch_tel,tch_create,tch_lastupdate
				) values(
					  :tacherId,:instituteId, :TeacherType, :TechnicalPosition,:TeacherEducational,:TeacherName,
					  'xxxx', :TeacherSex, :TeacherIDCard,
					  :TeacherEmail, :TeacherTelephone, :tchCreate, :tchLastupdate   
				)";
				
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="tacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="instituteId", value=depId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherType", value=TeacherType, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TechnicalPosition", value=tpId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TeacherEducational", value=eduId, cfsqltype="cf_sql_char" );
			
			queryObj.addParam( name="TeacherName", value=trim( event.getArg("TeacherName") ), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherSex", value=event.getArg("TeacherSex"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TeacherIDCard", value=trim( event.getArg("TeacherIDCard") ), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherEmail", value=trim( event.getArg("EmailAddress") ), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TeacherTelephone", value=trim( event.getArg("TeacherTelephone") ), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="tchCreate", value=date, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="tchLastupdate", value=date, cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			
			sql = "insert into t_user (
					  user_id,user_name,user_email,user_last_login,user_last_login_ip,user_pass,user_login
				)values (
					  :userId,:userName, :userEmail, :userLastLogin, :userLastLoginIp, :userPass, :userLogin 
				)";
				
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="userId", value=teacherId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="userName", value=trim( event.getArg("TeacherName") ), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="userEmail", value=trim( event.getArg("EmailAddress") ), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="userLastLogin", value=date, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="userLastLoginIp", value="127.0.0.1", cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="userPass", value=password, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="userLogin", value=loginName, cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			
			sql = "INSERT INTO t_groupuser(
					  user_id,group_id,ugid,date_create,date_lastupdate)
				values(
					  :userId,:groupId, :ugId, :dateCreate,:dateLastUpdate 
				)";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="userId", value=teacherId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="groupId", value="P001", cfsqltype="cf_sql_char" );
			queryObj.addParam( name="ugId", value=teacherId & ":" & "P001", cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="dateCreate", value=date, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="dateLastUpdate", value=date, cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			/* Pass Args */
			event.setArg("DepID", depId);
			event.setArg("TCHID", teacherId);
		}
		
	}

}

</cfscript>